<?php
# This file is part of "Central heating manager".

# "Central heating manager" is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# "Central heating manager" is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with "Central heating manager".  If not, see <http://www.gnu.org/licenses/>.



//header('Content-Type: text/html; charset=ISO-8859-1');

require_once('inc/config.inc.php');

// QQ variables
$nbHoraires = 6;
$periodes = array("S" => "Semaine", "W" => "Week-end");


// Enregistrement des infos
if($_POST['action'] == 'sauver'){

	// Récupération des infos sur la zone
	$zoneid	= (int)$_POST['zoneid'];
	$nom	= $_POST['nom'];
	$modeid	= $_POST['modeid'];
	$jusqua	= $_POST['jusqua'];
	
	// Enregistrement des infos
	$stmt = $conn->prepare("UPDATE zones SET nom=:nom, modeid=:modeid, jusqua=:jusqua WHERE id=:zoneid");
	$stmt->bindValue(':zoneid', $zoneid, SQLITE3_INTEGER);
	$stmt->bindValue(':nom', $nom, SQLITE3_TEXT);
	$stmt->bindValue(':modeid', $modeid, SQLITE3_TEXT);
	$stmt->bindValue(':jusqua', $jusqua, SQLITE3_TEXT);
	$stmt->execute();
	
	//Suppresion des horaires existants
	$sql = 'DELETE FROM horaires WHERE zoneid = '.$zoneid;
	$conn->exec($sql);
	
	// Enregistrement des infos sur les horaires	
	$stmt = $conn->prepare("INSERT INTO horaires (id, zoneid, periodeid, heure, modeid) VALUES (NULL, :zoneid, :periodeid, :heure, :modeid)");

	$horaires = array();
	foreach($periodes as $periodeid => $periode){
		for($i = 1; $i <= $nbHoraires; $i++){
			$heure = $_POST['heure'.$periodeid.$i];
			$modeid = $_POST['modeid'.$periodeid.$i];
			
			if ($heure != '' && $modeid != ''){
				$stmt->clear();
				$stmt->bindValue(':zoneid', $zoneid, SQLITE3_INTEGER);
				$stmt->bindValue(':periodeid', $periodeid, SQLITE3_TEXT);
				$stmt->bindValue(':heure', $heure, SQLITE3_TEXT);
				$stmt->bindValue(':modeid', $modeid, SQLITE3_TEXT);
				$stmt->execute();
			}
		}
	}

//exit;	
}

// On arrive sur la page à partir de la liste des zones
else {
	$zoneid = (int)$_GET['zoneid'];
}

$zone 		= getZones($zoneid);	
$horaires	= getHoraires($zoneid);
//print_r($horaires);
//exit();


affHeader(array(
			//'data-add-back-btn' => true,
			));

?>
<form method="post" action="zone.php">
<input type="hidden" name="action" value="sauver">
<input type="hidden" name="zoneid" value="<?php echo $zoneid; ?>">

<!-- Header -->
<div data-role="header"> 
	<h1><?php echo $zone['nom']; ?></h1>
	<a href="./" class="ui-btn-left" data-icon="home" data-iconpos="notext">Accueil</a>
	<input type="submit" class="ui-btn-right" data-icon="check" value="Sauver" />
</div>

<div data-role="content">
	<!-- Mode -->
	<div data-role="fieldcontain">
		<label for="modes">Mode actuel :</label>
		<?php
		affModes(array('modeid' => $zone['modeid'],));
		?>
	</div>	


	<!-- Jusqua -->
	<div data-role="fieldcontain" id="divjusqua">
		<label for="nom">Jusqu'à :</label>
		<input type="datetime" name="jusqua" id="jusqua" value="<?php echo $zone['jusqua']; ?>" />
	</div>	
	
	
	<!-- Horaires -->	
	<div data-role="fieldcontain">
		<label for="horaires">Horaires :</label>

		<div id="horaires" data-role="collapsible-set">
			<?php
			foreach($periodes as $periodeid => $periode){
			
				echo'<div data-role="collapsible" '.($periodeid == "S" ? ' data-collapsed="false"' : '').'>';
					echo '<h3>'.$periode.'</h3>';

					$horaires_periode = $horaires[$periodeid];
					
					for ($i = 1; $i <= $nbHoraires; $i++){
						
						// Si on a des horaires enregistrées, on prend le 1er élément du tableau puis on l'enlève de ce dernier jusqu'à épuisement des horaires
						if(count($horaires_periode) > 0){
							$heure = key($horaires_periode);
							$modeid = $horaires_periode[$heure];
							array_shift($horaires_periode);
						}
						// Plus d'horaires => valeurs vides
						else {
							$heure = '';
							$modeid = '';
						}
						
						// Affichage de l'horaire
						echo '<div id="bloc'.$i.'">';

						affModes(
								array(	
										'horaires' => true, 
										'suffix' => $periodeid.$i,
										'heure' => $heure,
										'modeid' => $modeid , 
										)
								);
						echo '</div>';
					}
				echo '</div>';
			}
			?>
		</div>
	</div>

	<!-- Nom -->
	<div data-role="fieldcontain">
		<label for="nom">Nom:</label>
		<input type="text" name="nom" id="nom" value="<?php echo $zone['nom']; ?>" />
	</div>	
</div>


</form>
<?php
affFooter();
?>